package mine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.swing.JOptionPane;

public class Controller {
	private Win win;
	private MineModel model;
	private int boardsize = 0;// size
	private int num = 0;// num
	private int aa = 0;// num旗子的数量
	private Timer timer;
	private MineLabel[][] board;

	public Controller(MineModel model, int boardsize, int num) {
		this.boardsize = boardsize;
		this.num = num;
		this.model = model;
		init();
	}

	private void init() {
		board = new MineLabel[boardsize][boardsize];
		win = new Win(this);
	}

	public void exit() {
		int val = JOptionPane.showConfirmDialog(win, "确定要离开？");
		if (val == JOptionPane.YES_OPTION) {
			System.exit(0);
		}
	}

	public void start() {
		win.getNumber().setText("  " + Integer.toString(num));
		model.initModel(boardsize, num);
		aa = num;
		showgame();
		if (timer != null) {
			timer.setGoon(false);
		}

		timer = new Timer(this);
		timer.start();
		win.toShow();
	}

	public void seesee(int x, int y) {
		if (model.see(x, y)) {
			for (int i = 0; i < model.getMine().length; i++) {
				for (int j = 0; j < model.getMine()[i].length; j++) {
					if ((x - i) * (x - i) + (y - j) * (y - j) == 1
							|| (x - i) * (x - i) + (y - j) * (y - j) == 2) {
						if (model.getChess()[i][j].equals("+")
								|| model.getChess()[i][j].equals("++")) {
							if (timer.isGoon()) {
								click(i, j);
							} else {
								return;
							}
						}
					}
				}
			}
		}
	}

	public void addLight(int i, int j) {
		if (model.getChess()[i][j].equals("+")) {
			model.getChess()[i][j] = "++";
		}
		showgame();
	}

	public void cutLight(int i, int j) {
		if (model.getChess()[i][j].equals("++")) {
			model.getChess()[i][j] = "+";
		}
		showgame();
	}

	public void click(int i, int j) {
		if(timer!=null&&!timer.isGoon()){
			return;
		}
		model.decide(i, j);
		showgame();
		if (model.isWin(aa)) {
			showgame();
			timer.setGoon(false);
			int val = JOptionPane.showConfirmDialog(win, "恭喜，你赢了！");
			if (val == JOptionPane.YES_OPTION) {
				timer = new Timer(this);
				start();
			}
		}
		if (model.isfail()) {
			timer.setGoon(false);
			new Thread() {
				public void run() {
					int val = JOptionPane.showConfirmDialog(win,
							"你输了，是否重新开始游戏？");
					if (val == JOptionPane.YES_OPTION) {
						if(timer!=null){
							timer.setGoon(false);
						}
						timer = new Timer(Controller.this);
						Controller.this.start();
					}
				};

			}.start();

		}
	}

	private void showgame() {
		String[][] c = model.getChess();
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c[i].length; j++) {
				String str = c[i][j];
				board[i][j].setmine(str);
			}
		}
	}

	public void changeLVL(OptionWin change) {

		if (change.getText1().getText().matches("[0-9]+")
				&& change.getText1().getText().matches("[0-9]+")) {

			int a = Integer.parseInt(change.getText1().getText());
			int b = Integer.parseInt(change.getText2().getText());
			change.dispose();
			win.dispose();
			timer.setGoon(false);
			if (a > 20) {
				a = 20;
			}
			if (a < 3) {
				a = 3;
			}
			MineModel minem = new MineModel(a, b);
			new Controller(minem, a, b).start();
		} else {
			JOptionPane.showMessageDialog(change, "输入数据必须为数字类型！");
			change.getText1().setText("");
			change.getText2().setText("");
		}

	}

	public void addFlag(int i, int j) {
		if(!timer.isGoon()){
			return;
		}
		model.addf(i, j);
		String[][] c = model.getChess();
		if (c[i][j].equals("+") || c[i][j].equals("++")) {
			win.getNumber().setText("  " + Integer.toString(++aa));
		} else if (c[i][j].equals("%")) {
			win.getNumber().setText("  " + Integer.toString(aa));
		} else {
			if (board[i][j].getIcon() == MineLabel.zone
					|| board[i][j].getIcon() == MineLabel.zone111) {
				win.getNumber().setText("  " + Integer.toString(--aa));
			}

		}
		showgame();
		if (model.isWin(aa)) {
			showgame();
			timer.setGoon(false);
			int val = JOptionPane.showConfirmDialog(win, "恭喜，你赢了！再来一次？");
			if (val == JOptionPane.YES_OPTION) {
				timer.setGoon(false);
				timer = new Timer(this);
				start();
			}
		}
		
		if (model.isfail()) {
			timer.setGoon(false);
			int val = JOptionPane.showConfirmDialog(win, "你输了，是否重新开始游戏？");
			if (val == JOptionPane.YES_OPTION) {
				timer.setGoon(false);
				timer = new Timer(this);
				start();
			}
		}
	}
	
	public void getHelp() {
		HelpWin helpWin = new HelpWin(this);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(
					getClass().getResourceAsStream("help.txt"), "GBK")); 
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		StringBuilder text = new StringBuilder();
		String temp;
		try {
			while ((temp = reader.readLine()) != null) {
				text.append(temp);
				text.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		helpWin.getTextArea().setText(text.toString());
		helpWin.getTextArea().setCaretPosition(0);
		helpWin.toShow();
	}

	public Win getWin() {
		return win;
	}

	public void setWin(Win win) {
		this.win = win;
	}

	public MineModel getModel() {
		return model;
	}

	public void setModel(MineModel model) {
		this.model = model;
	}

	public int getBoardsize() {
		return boardsize;
	}

	public void setBoardsize(int boardsize) {
		this.boardsize = boardsize;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getAa() {
		return aa;
	}

	public void setAa(int aa) {
		this.aa = aa;
	}

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	public MineLabel[][] getBoard() {
		return board;
	}

	public void setBoard(MineLabel[][] board) {
		this.board = board;
	}

}
